<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;
use think\Exception;
use think\exception\PDOException;

/**
 * 订单支付表 日志类
 *
 * @icon fa fa-circle-o
 */
class ExpressPay extends Backend
{
    
    /**
     * ExpressPay模型对象
     * @var \app\admin\model\ExpressPay
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\ExpressPay;
        $this->view->assign("payTypeList", $this->model->getPayTypeList());
        $this->view->assign("payStateList", $this->model->getPayStateList());
        $this->view->assign("statusList", $this->model->getStatusList());
    }

    public function import()
    {
        parent::import();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model->where(['pay_type'=>3])->where("pay_state",'in',[1,2,3,4])
                //->($where)
                ->order($sort, $order)
                ->paginate($limit);
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 创建批次并加入批次 && 加入批次
     */
    public function edit($ids=null){
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        if($this->request->isPost()){
            Db::startTrans();
            try{

//                dump($_GET);
//                dump($_POST);

                $info = $this->model->where('id','in',$ids)->where(['pay_type'=>3])->find();
//                dump($this->model);
//                dump($info);
//                dump($info->pay_state);
                if($info->pay_state == 3){
                    if($_POST['row']['pay_state'] == 1){   //支付审核成功
                        $info->save([
                            'pay_state' => '1',
                            'actual_payment' => $info->price,
                            'paytime' => time(),
                        ]);
                        Db::name('express_package')->where('id','in',$info->order_ids)->update([
                            'status' => 'successpay',
                            'payStatus'=>1
                        ]);
                    }elseif($_POST['row']['pay_state'] == 4){  //支付审核失败
                        $info->save([
                            'pay_state' => '4',
                        ]);
                        Db::name('express_package')->where('id','in',$info->order_ids)->update([
                            'status' => 'waitingpay',
                        ]);
                    }
                }


                Db::commit();
            }catch (PDOException $e){
                Db::rollback();
                $this->error($e->getMessage());
            }catch (Exception $e){
                Db::rollback();
                $this->error($e->getMessage());
            }
            $this->success('操作成功');
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }
    

}
